const tagModel = require('../model/tagModel')
const { SUCCESS_RESPONSE, FAIL_RESPONSE } = require('../config/constant')

// 获取标签名称数据
exports.getTagNameData = async (req, res) => {
  let result = await tagModel.getTagName()
  res.json(SUCCESS_RESPONSE('修改标签成功', result))
}

// 编辑标签
exports.editTagData = async (req, res) => {
  let tagData = req.body
  await tagModel.editTag(tagData)
  res.json(SUCCESS_RESPONSE('修改标签成功'))
}

// 获取编辑标签的数据
exports.editedTagData = async (req, res) => {
  let { tag_id } = req.query
  let editedTagData = await tagModel.editedTag(tag_id)
  res.json(SUCCESS_RESPONSE('获取编辑标签的数据成功', editedTagData))
}

// 删除选择标签
exports.delSelTagData = async (req, res) => {
  let { tag_ids } = req.body
  await tagModel.delSelTag(tag_ids)
  res.json(SUCCESS_RESPONSE('删除选中标签成功'))
}

// 删除标签
exports.delTagData = async (req, res) => {
  let { tag_id } = req.body
  await tagModel.delTag(tag_id)
  res.json(SUCCESS_RESPONSE('删除标签成功'))
}

// 校验标签名称是否重复
exports.verifyTagName = async (req, res) => {
  let { tag_id, tag_Name } = req.query
  let result = await tagModel.verifyName(tag_id, tag_Name)
  if (result > 0) {
    res.json(FAIL_RESPONSE('00004', '标签名已存在,请更换一个'))
  }
  res.json(SUCCESS_RESPONSE('添加标签成功'))
}

// 添加标签
exports.addTagData = async (req, res) => {
  let tagData = req.body
  await tagModel.addTag(tagData)
  res.json(SUCCESS_RESPONSE('添加标签成功'))
}

// 获取标签的数据和数量
exports.getTagData = async (req, res) => {
  let { page, pagesize, tag_name, is_show } = req.query
  let tagData = await tagModel.getTag(page, pagesize, tag_name, is_show)
  let tagCount = await tagModel.getCount(tag_name, is_show)
  res.json(SUCCESS_RESPONSE('获取标签数据成功', { tagData, tagCount }))
}
